// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections.Generic;

namespace Nethermind.Blockchain.FullPruning;

/// <summary>
/// Allows to have multiple <see cref="IPruningTrigger"/>s.
/// </summary>
public class CompositePruningTrigger : IPruningTrigger, IDisposable
{
    private readonly List<IPruningTrigger> _triggers = [];

    /// <summary>
    /// Adds new <see cref="IPruningTrigger"/> to be watched.
    /// </summary>
    /// <param name="trigger">trigger to be watched</param>
    public void Add(IPruningTrigger trigger)
    {
        _triggers.Add(trigger);
        trigger.Prune += OnPrune;
    }

    private void OnPrune(object? sender, PruningTriggerEventArgs e)
    {
        Prune?.Invoke(sender, e);
    }

    /// <inheritdoc />
    public event EventHandler<PruningTriggerEventArgs>? Prune;

    public void Dispose()
    {
        foreach (IPruningTrigger trigger in _triggers)
        {
            trigger.Prune -= OnPrune;
            if (trigger is IDisposable d)
            {
                d.Dispose();
            }
        }
        _triggers.Clear();
    }
}
